package com.huqing.icu.service.admin.impl;

import com.huqing.icu.SpringBootApplicationContextUtils;
import com.huqing.icu.base.PagingDto;
import com.huqing.icu.dto.CommunityPostDto;
import com.huqing.icu.dto.CommunityPostQueryDto;
import com.huqing.icu.inout.req.admin.AdminCommunityPostApproveReq;
import com.huqing.icu.inout.req.admin.AdminCommunityPostListQueryReq;
import com.huqing.icu.inout.resp.admin.AdminCommunityPostListQueryResp;
import com.huqing.icu.mapper.CommunityPostMapper;
import com.huqing.icu.mapper.CommunityPostStatisticsMapper;
import com.huqing.icu.model.CommunityPost;
import com.huqing.icu.service.admin.IAdminCommunityPostService;
import com.huqing.icu.utils.CopyUtils;
import com.huqing.icu.utils.PageUtils;
import com.huqing.icu.utils.ParamValidateUtils;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.Collections;
import java.util.List;

/**
 * @Description 后台社区帖子接口实现类
 * @Author huqing
 * @Date 2025/7/7 21:08
 **/
@Service
public class AdminCommunityPostServiceImpl implements IAdminCommunityPostService {

    @Resource
    private CommunityPostMapper communityPostMapper;
    @Resource
    private CommunityPostStatisticsMapper communityPostStatisticsMapper;


    /**
     * 后台查询帖子列表
     *
     * @param queryReq
     * @return
     */
    @Override
    public PagingDto<AdminCommunityPostListQueryResp> queryList(AdminCommunityPostListQueryReq queryReq) {
        CommunityPostQueryDto communityPostQueryDto = CopyUtils.copy(queryReq, CommunityPostQueryDto.class);
        int startIndex = PageUtils.getPageStartIndex(queryReq.getPageIndex(), queryReq.getPageSize());
        communityPostQueryDto.setStartIndex(startIndex);

        //1、根据条件查询帖子数量
        Integer count = communityPostMapper.getNumByCondition(communityPostQueryDto);
        if (count == 0) {
            return new PagingDto<>(Collections.emptyList(), 0, 0);
        }

        //2、根据条件查询帖子列表
        List<CommunityPostDto> communityPostDtoList = communityPostMapper.selectByCondition(communityPostQueryDto);

      /*  //3、查询帖子的点赞量、收藏了等信息
        List<Long> postIdList = communityPostDtoList.stream().map(CommunityPostDto::getId).collect(Collectors.toList());
        List<CommunityPostStatistics> communityPostStatisticsList = communityPostStatisticsMapper.selectBatchIds(postIdList);
        Map<Long, CommunityPostStatistics> postStatisticsMap = communityPostStatisticsList.stream().collect(Collectors.toMap(CommunityPostStatistics::getPostId, p -> p);*/


        List<AdminCommunityPostListQueryResp> respList = CopyUtils.copyOriginListToDestList(communityPostDtoList, AdminCommunityPostListQueryResp.class);
        //4、遍历每个响应类，填充数据
        for (AdminCommunityPostListQueryResp resp : respList) {

        }

        return new PagingDto<>(respList, PageUtils.getTotalPage(count, queryReq.getPageSize()), count);
    }

    /**
     * 批量审核
     *
     * @param approveReq
     * @param userId
     * @return
     */
    @Override
    public Integer batchApprove(AdminCommunityPostApproveReq approveReq, Long userId) {
        for (Long id : approveReq.getIdList()) {
            SpringBootApplicationContextUtils.getBean(IAdminCommunityPostService.class).approveOne(id, approveReq.getApproveStatus(), approveReq.getApproveContent(), userId);
        }
        return 1;
    }

    /**
     * 审核单个帖子
     *
     * @param id
     * @param approveStatus
     * @param approveContent
     * @param userId
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer approveOne(Long id, Integer approveStatus, String approveContent, Long userId) {

        //1、先查询这个帖子
        CommunityPost communityPost = communityPostMapper.selectById(id);
        if (communityPost == null) {
            ParamValidateUtils.throwObjectNotExistExp("帖子不存在，请检查");
        }

        //2、修改审核状态
        communityPost.setApproveStatus(approveStatus);
        communityPost.setApproveContent(approveContent);
        communityPost.setApproveUserId(userId);
        communityPost.setApproveTime(new Timestamp(System.currentTimeMillis()));
        communityPostMapper.updateById(communityPost);

        //3、审核通过或不通过，可以发微信消息
        return 1;
    }
}
